package com.chaodroid.flyui.view.text;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by CHEN on 2017/12/12.
 * 自定义TextView,实现上下左右图标的点击监听功能
 */

public class DrawableTextView extends AppCompatTextView {

    public final static int DRAWABLE_LEFT = 0;  //左图片
    public final static int DRAWABLE_TOP = 1;   //上图片
    public final static int DRAWABLE_RIGHT = 2; //右图片
    public final static int DRAWABLE_BOTTOM = 3;//下图片

    protected Drawable mDrawableLeft;
    protected Drawable mDrawableTop;
    protected Drawable mDrawableRight;
    protected Drawable mDrawableBottom;
    protected DrawableClickListener mDrawableClickListener;

    public DrawableTextView(Context context) {
        this(context, null);
    }

    public DrawableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //父类中有默认样式设置
        init();
    }

    public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mDrawableLeft = getCompoundDrawables()[DRAWABLE_LEFT];
        mDrawableRight = getCompoundDrawables()[DRAWABLE_RIGHT];
        mDrawableTop = getCompoundDrawables()[DRAWABLE_TOP];
        mDrawableBottom = getCompoundDrawables()[DRAWABLE_BOTTOM];
    }

    //定义Drawable点击监听接口
    public interface DrawableClickListener {
        void onDrawableClick(View view, int position);
    }

    //定义Drawable点击监听的set方法
    public void setDrawableClickListener(DrawableClickListener listener) {
        this.mDrawableClickListener = listener;
    }

    //重写onTouchEvent方法
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                if (mDrawableClickListener != null) {
                    if (mDrawableLeft != null && clickLeftDrawable(event)) {
                        mDrawableClickListener.onDrawableClick(this, DRAWABLE_LEFT);
                        return true;
                    }
                    if (mDrawableRight != null && clickRightDrawable(event)) {
                        mDrawableClickListener.onDrawableClick(this, DRAWABLE_RIGHT);
                        return true;//返回值一定要是true，否则无法响应
                    }
                    if (mDrawableTop != null && clickToptDrawable(event)) {
                        mDrawableClickListener.onDrawableClick(this, DRAWABLE_TOP);
                        return true;//返回值一定要是true，否则无法响应
                    }
                    if (mDrawableBottom != null && clickBottomDrawable(event)) {
                        mDrawableClickListener.onDrawableClick(this, DRAWABLE_BOTTOM);
                        return true;//返回值一定要是true，否则无法响应
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    //判断点击左边的Drawable
    //判断的依据是获取点击区域相对于屏幕的x值小于控件最左边界加上左侧控件的宽度
    public boolean clickLeftDrawable(MotionEvent event) {
        return event.getX() >= getLeft() &&
                event.getX() <= (getLeft() + getPaddingLeft() + mDrawableLeft.getBounds().width());
    }

    //判断点击右边的Drawable
    //判断的依据是获取点击区域相对于屏幕的x值大于控件的最右边界减去右侧控件的宽度
    public boolean clickRightDrawable(MotionEvent event) {
        return event.getX() <= getRight() &&
                event.getX() >= (getRight() - getPaddingRight() - mDrawableRight.getBounds().width());
    }

    //判断点击上边的Drawable
    public boolean clickToptDrawable(MotionEvent event) {
        return event.getY() <= getTop() &&
                event.getY() >= (getTop() - getPaddingTop() - mDrawableTop.getBounds().height());
    }

    //判断点击下边的Drawable
    public boolean clickBottomDrawable(MotionEvent event) {
        return event.getY() >= getBottom() &&
                event.getY() <= (getBottom() + getPaddingBottom() + mDrawableBottom.getBounds().height());
    }

}
